

//
//  UIButton+Extension.swift
//  微博01
//
//  Created by 牛威 on 15/12/6.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit


extension UIButton {


  convenience  init(imgName:String,backImage:String? = nil) {
    
    self.init()
    
    //设置普通图片
    
    setImage(UIImage(named: imgName), forState: UIControlState.Normal)
    
    //设置高亮图片
    
    
    setImage(UIImage(named:"\(imgName)_highlighted"), forState: UIControlState.Highlighted)
    
    
    //设置背景图片
    
    
    if let back = backImage {
    
        setBackgroundImage(UIImage(named:back), forState: UIControlState.Normal)
        
        setBackgroundImage(UIImage(named:"\(back)_highlighted"), forState: UIControlState.Highlighted)
        
    
    }
    
    //设置大小
    
    sizeToFit()

    
    
    }
    
    convenience init(title:String,backImageName:String,color:UIColor){
        
        //便利构造函数 不需要返回值 程序结束就创建完成 用self.init() 进行初始化
        //便利构造函数还提供智能提示
    
//                let btn = UIButton() //这里不需要实例化

        
        self.init()
        
        setTitle(title, forState: .Normal)
        
        setTitleColor(color, forState: .Normal)
        
        setBackgroundImage(UIImage(named:backImageName), forState: .Normal)
    
    }
    
    convenience init(title:String,imageName:String?,color:UIColor,fontSize:CGFloat,backColor:UIColor? = nil){
        
        
        self.init()
        
        setTitle(title, forState: .Normal)
        
        setTitleColor(color, forState: .Normal)
        
        if let imageName = imageName {
        
            setImage(UIImage(named: imageName), forState: .Normal)

        }
        
        backgroundColor = backColor
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        sizeToFit()
    }

    
   
    


}
